<template>
  <div class="ele-body">
    <el-card
      shadow="never"
      v-loading="loading">
      <ele-pro-table
        ref="table"
        row-key="dept_id"
        :datasource="url"
        :where="where"
        :columns="columns"
        default-expand-all
        :need-page="false"
        height="calc(100vh - 199px)"
        :parse-data="parseData">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)">添加
          </el-button>
          <el-button
            @click="expandAll"
            class="ele-btn-icon"
            size="small">展开全部
          </el-button>
          <el-button
            @click="foldAll"
            class="ele-btn-icon"
            size="small">折叠全部
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-plus"
            @click="openEdit(null, row.dept_id)">添加
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <org-edit
      :visible.sync="showEdit"
      :data="editData"
      :organization-list="data"
      @done="reload"/>
  </div>
</template>

<script>
import OrgEdit from './org-edit';

export default {
  name: 'SystemOrganization',
  components: {OrgEdit},
  data() {
    return {
      // 加载状态
      loading: true,
      // 列表数据
      url:"/app.sys.dept.lst",
      // 表格列配置
      columns: [
        {
          prop: 'dept_name',
          label: '机构名称',
          showOverflowTooltip: true,
          minWidth: 110,
        },
        {
          prop: 'dept_full_name',
          label: '机构全称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'dept_code',
          label: '机构代码',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 80
        },
        {
          prop: 'sort_number',
          label: '排序',
          align: 'center',
          showOverflowTooltip: true,
          width: 60
        },
        {
          prop: 'comments',
          label: '备注',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 190,
          align: 'center',
          resizable: false,
          slot: 'action'
        }
      ],
      data: [],
      // 表格搜索条件
      where: {},
      // 选中数据
      current: null,
      // 是否显示表单弹窗
      showEdit: false,
      // 编辑回显数据
      editData: null
    };
  },
  mounted() {
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 解析接口返回数据 */
    parseData(res) {
      this.loading = false;
      res.data = this.$util.toTreeData(res.data, 'dept_id', 'parent_id');
      this.data = res.data;
      return res;
    },
    /* 显示编辑 */
    openEdit(item,pid) {
      this.editData = item || {parent_id: pid};
      this.showEdit = true;
    },
    /* 展开全部 */
    expandAll() {
      this.$refs.table.data.forEach(d => {
        this.$refs.table.toggleRowExpansion(d, true);
      });
    },
    /* 折叠全部 */
    foldAll() {
      this.$refs.table.data.forEach(d => {
        this.$refs.table.toggleRowExpansion(d, false);
      });
    },
    /* 删除 */
    remove(row) {
      if (row.children && row.children.length > 0) {
        this.$message.error('请先删除子节点');
        return;
      }
      const loading = this.$loading({lock: true});
      this.$http.post('/app.sys.dept.del',{dept_id:row.dept_id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
.org-tree-group {
  border: 1px solid hsla(0, 0%, 60%, .15);
  height: calc(100vh - 199px);
  box-sizing: border-box;
  overflow: auto;
}

.org-tree-group ::v-deep .el-tree-node__content {
  height: 40px;
}

.org-tree-group ::v-deep .el-tree-node__content > .el-tree-node__expand-icon {
  margin-left: 10px;
}
</style>
